package com.tarena.luban.worker.server.infrastructure.repo.impl;

import com.tarena.luban.worker.server.dao.api.WorkerCategoryDAO;
import com.tarena.luban.worker.server.domain.bo.WorkerCategoryBO;
import com.tarena.luban.worker.server.domain.repository.WorkerCategoryRepository;
import com.tarena.luban.worker.server.infrastructure.converter.WorkerCategoryConverter;
import com.tarena.tp.luban.worker.po.WorkerCategory;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCategoryParam;
import com.tarena.tp.luban.worker.server.dto.param.WorkerCreateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Repository
public class WorkerCategoryRepoImpl implements WorkerCategoryRepository {
    @Autowired(required = false)
    private WorkerCategoryDAO workerCategoryDAO;
    //注入转化器,代码公用提取
    @Autowired
    private WorkerCategoryConverter workerCategoryConverter;
    @Override
    public void delete(Long userId) {
        workerCategoryDAO.delete(userId);
    }
    //worker_category
    @Override
    public void saveWorkerCategory(WorkerCreateParam workerCreateParam) {
        // 将参数 中List<WorkerCategoryParam> 转化成 List<WorkerCategory>
        List<WorkerCategoryParam> workerCategoryParams=workerCreateParam.getWorkerCategoryParams();
        List<WorkerCategory> workerCategories=new ArrayList<>();
        for (WorkerCategoryParam workerCategoryParam : workerCreateParam.getWorkerCategoryParams()) {
            //创建一个do对象
            WorkerCategory workerCategory =
                    workerCategoryConverter.param2po(workerCategoryParam);
            workerCategory.setUserId(workerCreateParam.getUserId());
            workerCategories.add(workerCategory);//TODO 缺少 通用字段封装 6个
        }
        // 调用持久层接口方法 批量新增 
        // insert into worker_category (字段) values (第一个元素),(第二个元素)
        workerCategoryDAO.insertCategories(workerCategories);
    }

    @Override
    public List<WorkerCategoryBO> getCategories(Long userId) {
        List<WorkerCategory> categories=workerCategoryDAO.selectCategories(userId);
        return workerCategoryConverter.poList2BoList(categories);
    }
}
